package com.situ.controller;

import com.situ.context.BaseContext;
import com.situ.domain.dto.SaveOrderDTO;
import com.situ.domain.entity.TOrder;
import com.situ.domain.vo.OrderAndItemVO;
import com.situ.service.OrderItemService;
import com.situ.service.OrderService;
import com.situ.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
@Tag(name = "订单接口")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    private final OrderItemService orderItemService;

    @PostMapping("/saveOrder")
    @Operation(summary = "保存订单接口")
    public Result<String> saveOrder (@RequestBody SaveOrderDTO saveOrderDTO) {
        log.info("保存订单接口:{}", saveOrderDTO);
        String orderId = orderService.saveOrder(saveOrderDTO);
        return Result.success(200, "保存成功", orderId);
    }


    @GetMapping("/getOrderById")
    @Operation(summary = "查询订单接口")
    public Result<TOrder> getOrderById (String orderId) {
        log.info("查询订单接口:{}", orderId);
        TOrder order = orderService.lambdaQuery()
                .eq(TOrder::getOid, orderId)
                .eq(TOrder::getUid, BaseContext.getCurrentId())
                .one();
        return Result.success(200, "查询成功", order);
    }


    /**
     * 查询当前用户订单列表
     * @return
     */
    @GetMapping("/getOrderList")
    @Operation(summary = "查询当前用户订单列表")
    public Result<List<TOrder>> getOrderList () {
        log.info("查询订单列表接口");
        List<TOrder> orderList = orderService.lambdaQuery()
                .eq(TOrder::getUid, BaseContext.getCurrentId())
                .list();
        return Result.success(200, "查询成功", orderList);
    }


    @GetMapping("/getOrderListByUserState")
    @Operation(summary = "查询订单列表接口")
    public Result<List<OrderAndItemVO>> getOrderListByUserState (Integer state) {
        log.info("查询订单列表接口:{}",state);
        List<OrderAndItemVO> list = orderService.getOrderListByUserState(state);
        return Result.success(200, "查询成功", list);
    }
}
